﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Spore.Validator;
using System.IO;
using DevExpress.XtraEditors;


namespace Client
{
    public partial class VipAdd : BasePopForm
    {
       List<QunYing.Model.VipServices> services;
       List<QunYing.Model.VipRank> vipRank;

        public VipAdd()
        {
            InitializeComponent();
            this.NecessaryRemark(this.txt_会员ID, this.txt_经销商, this.txt_水族箱编号,
                this.txt_通信地址, this.txt_移动电话, this.txt_固定电话, this.txt_服务项,
                this.txt_VipCardID, this.txt_name, this.txt_省份, this.txt_城市,this.dte_到期时间,
                this.dte_入会时间);
            this.cbe_会员级别.SelectedIndexChanged += new EventHandler(cbe_会员级别_SelectedIndexChanged);
            this.txt_经销商.Text = GlobalInfo.UserLoginInfo.用户ID;
            this.dte_入会时间.DateTime = DateTime.Now;
            this.FormValidator.SetControlValitors(txt_会员ID, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(txt_省份, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(txt_水族箱编号, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.cbe_会员级别, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.txt_VipCardID, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.txt_固定电话, ValidatorDefaults.Phone);
            this.FormValidator.SetControlValitors(this.txt_移动电话, ValidatorDefaults.Phone);
            this.FormValidator.SetControlValitors(this.txt_通信地址, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.txt_城市, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.txt_email, ValidatorDefaults.Email);
            this.FormValidator.SetControlValitors(this.txt_name, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.dte_入会时间, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.dte_到期时间, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.txt_地址图片, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.txt_缴费金额, ValidatorDefaults.Integer);
        }

        void cbe_会员级别_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.txt_服务项.BeginUpdate();
            this.txt_服务项.Items.Clear();
            string[] strs = vipRank.First((v) => { return v.等级名称 == cbe_会员级别.Text; }).基本服务条款.Split(",".ToCharArray(),StringSplitOptions.RemoveEmptyEntries);
             foreach(var v in strs)
             {
                 var temp = services.First((vv) => { return vv.ID.ToString() == v; });
                 this.txt_服务项.Items.Add(temp, true);
             }
            this.txt_服务项.EndUpdate();
        }

        protected override void OnLoad(EventArgs e)
        {
            this.services = (new QunYing.BLL.VipServices()).GetModelList(string.Empty);
            this.vipRank = (new QunYing.BLL.VipRank()).GetModelList(string.Empty);
            
            base.OnLoad(e);
        }

        private void btn_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            if (!this.FormValidator.ValidAll()) return;
            try
            {
                if (!File.Exists(txt_地址图片.Text))
                {
                    this.ep.SetError(this.txt_地址图片, "请设置地址图片文件！");
                    return;
                }
                QunYing.BLL.VipInfo vif = new QunYing.BLL.VipInfo();
                QunYing.BLL.UserLoginInfo bll_userLoginInfo = new QunYing.BLL.UserLoginInfo();
                
                var v = vif.GetModel(this.txt_VipCardID.Text.Trim());
                if (v != null)
                {
                    GlobalInfo.MsgBoxShowError("已存在该用户！");
                    return;
                }
                if (bll_userLoginInfo.Exists(this.txt_会员ID.Text.Trim()))
                {
                    GlobalInfo.MsgBoxShowError("已存在该用户！");
                    return;
                }
                
                if (vif.GetModelList(string.Format("用户ID='{0}'", this.txt_会员ID.Text.Trim())).Count != 0)
                {
                    GlobalInfo.MsgBoxShowError("已存在该用户！");
                    return;
                }
                string[] devices = this.txt_水族箱编号.Text.Trim().Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                QunYing.BLL.ICSA_Device ser = new QunYing.BLL.ICSA_Device();
                foreach (var vv in devices)
                {
                    QunYing.Model.ICSA_Device model = ser.GetModel(vv);
                    if (model == null)
                    {
                        GlobalInfo.MsgBoxShowError("不存在设备：" + vv);
                        return;
                    }
                    else
                    {
                        if (model.是否已售 != null)
                        {
                            if (model.是否已售.Value)
                            {
                                GlobalInfo.MsgBoxShowError("设备已出售:" + model.ICSA设备ID);
                                return;
                            }
                        }
                    }
                }
                foreach (var vv in devices)
                {
                    QunYing.Model.ICSA_Device device = ser.GetModel(vv);
                    device.是否已售 = true;
                    ser.Update(device);
                }
                try
                {
                    QunYing.Common.IImageOperator opera = QunYing.Common.ClientProxyFactory.GetProxy<QunYing.Common.IImageOperator>(QunYing.Common.GlobalInfo.ServiceImageOperationVip);
                    opera.UpLoad(GlobalInfo.UserLoginInfo.用户ID, new string[] { this.txt_VipCardID.Text + ".jpg" }, new Bitmap[] { (Bitmap)Bitmap.FromFile(txt_地址图片.Text) });
                }
                catch (Exception ex)
                {
                    QunYing.Common.Loger.Log.Error("上传图片时发生错误:" + ex.Message);
                }
                QunYing.Model.VipInfo vipInfo = new QunYing.Model.VipInfo();
                vipInfo.到期时间 = this.dte_到期时间.DateTime;
                vipInfo.入会时间 = this.dte_入会时间.DateTime;
                vipInfo.电邮地址 = this.txt_email.Text.Trim();
                vipInfo.固定电话 = this.txt_固定电话.Text.Trim();
                vipInfo.会员级别 = this.cbe_会员级别.Text;
                vipInfo.会员卡号 = this.txt_VipCardID.Text;
                vipInfo.经销商编号 = this.txt_经销商.Text;
                vipInfo.审核状态 = "未审核";
                vipInfo.水族箱设备ID = this.txt_水族箱编号.Text.Trim();
                vipInfo.其他信息 = this.txt_其他信息.Text;
                string services = string.Empty;
                foreach (var vv in this.txt_服务项.CheckedItems)
                {
                    services += this.services.FirstOrDefault((ee) => ee.服务项 == vv.ToString()).ID.ToString() + ",";
                }
                services = services.TrimEnd(',');
                vipInfo.所选服务项 = services;
                vipInfo.所在城市 = this.txt_城市.Text;
                vipInfo.所在省份 = this.txt_省份.Text;
                vipInfo.通信地址 = this.txt_通信地址.Text;
                vipInfo.移动电话 = this.txt_移动电话.Text;
                vipInfo.用户ID = this.txt_会员ID.Text;
                vipInfo.用户姓名 = this.txt_name.Text;
                vipInfo.首次缴费 = int.Parse(this.txt_缴费金额.Text.Trim());
                vif.Add(vipInfo);
                GlobalInfo.MsgBoxShow("会员申请已提交，请等待审核。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                this.Close();
            }
            catch (Exception ex)
            {
                GlobalInfo.MsgBoxShowError("程序出现错误：" + ex.Message);
            }
        }

        private void btn_图片_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.CheckPathExists = true;
            ofd.Multiselect = false;
            ofd.Filter = string.Format("{0}|{1};{2};{3}", "图片文件", "*.jpg", "*.pgn", "*.bmp");
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                this.txt_地址图片.Text = ofd.FileName;
                this.pet_地址图片.Image = Bitmap.FromFile(txt_地址图片.Text);
            }
        }
    }
}
